package cn.sq.musicserver.playlist.controller;


import cn.hutool.json.JSONObject;
import cn.sq.musicserver.config.ResponseBean;
import cn.sq.musicserver.config.SQConstant;
import cn.sq.musicserver.config.StatusCode;
import cn.sq.musicserver.config.base.BaseController;
import cn.sq.musicserver.config.base.BaseSelectSymbol;
import cn.sq.musicserver.config.entity.SymbolPlus;
import cn.sq.musicserver.exception.FileChangeExeception;
import cn.sq.musicserver.music.entity.SqMusic;
import cn.sq.musicserver.playlist.entity.SqMuiscPlay;
import cn.sq.musicserver.playlist.entity.SqPlay;
import cn.sq.musicserver.playlist.service.ISqPlayService;
import cn.sq.musicserver.playlist.service.impl.SqMuiscPlayServiceImpl;
import cn.sq.musicserver.utils.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 歌曲播放列表 前端控制器
 * </p>
 *
 * @author SQ
 * @since 2020-12-28
 */
@RestController

@RequestMapping("/playlist")
@Api(value = "我的收藏", tags = {"播放列表"})
public class SqPlayController extends BaseController<SqPlay, ISqPlayService> {


    @RequestMapping("/list_by_page")
    @ApiOperation(value = "根据条件查询播放列表")
    @RequiresAuthentication
    @ApiOperationSupport(params = @DynamicParameters(name = "页面对象(IPage扩展对象)", properties = {
            @DynamicParameter(name = "pagesize", value = "每页长度", example = "10", required = true, dataTypeClass = String.class),
            @DynamicParameter(name = "pageindex", value = "第几页", example = "1", required = true, dataTypeClass = String.class),
            @DynamicParameter(name = "ascs", value = "正序字段", required = true, dataTypeClass = String.class),
            @DynamicParameter(name = "desc", value = "倒叙字段", required = true, dataTypeClass = String.class),
    }))
    public ResponseBean list_by_page(@RequestBody HashMap<String, String> map) {
        SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        IPage<SqPlay> sqPlayIPage = super.base_get_list_by_page(symbolPlus);
        return StatusCode.successResponseBean(sqPlayIPage);
    }

    /**
     * 添加
     *
//     * @param data
     * @return 添加结果
     */
    @RequiresAuthentication
    @ApiOperation(value = "添加")
    @RequestMapping("save")
    public ResponseBean save(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws FileChangeExeception {
        System.out.println(request.getParameterMap());
        JSONObject data = new JSONObject(request.getParameterMap().get("data")[0]);
        String newpath = UUID.randomUUID().toString() + FileUtil.getFileSuffix(file.getOriginalFilename());
        SqPlay sqPlay = new SqPlay();
        String playName="";
        String userUUID ="";
        if (FileUtil.multipartFileToFile(file, SQConstant.SYSTEM_IMAGE_DIR+ File.separator +newpath)) {

            playName = data.get("playName").toString();
            userUUID = data.get("userUUID").toString();
            sqPlay.setUserUUID(userUUID);
            sqPlay.setPlayImage(newpath);
            sqPlay.setPlayName(playName);
            sqPlay.setPlayTime(LocalDateTime.now());
        }

        QueryWrapper<SqPlay> sqPlayQueryWrapper = new QueryWrapper<SqPlay>().eq("UserUUID", userUUID).orderByDesc();
        Page<SqPlay> pageinfo = new Page<SqPlay>(1, 1);
        pageinfo.addOrder(OrderItem.desc("PlayIndex"));

        Page<SqPlay> page = super.sService.page(pageinfo, sqPlayQueryWrapper);
        if (page.getTotal()==0){
            sqPlay.setPlayIndex("1");
        }else{
            sqPlay.setPlayIndex(String.valueOf(Integer.parseInt(page.getRecords().get(0).getPlayIndex())+1));
        }
        boolean add = super.add(sqPlay);
        return StatusCode.successResponseBean(add);
    }

    @RequiresAuthentication
    @ApiOperation(value = "删除")
    @RequestMapping("delete")
        public ResponseBean delete(@RequestBody HashMap<String, String> map) {
            SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        boolean del = super.del(symbolPlus.getQueryWrapper());
        return StatusCode.successResponseBean(del);
    }


    @RequiresAuthentication
    @ApiOperation(value = "修改")
    @RequestMapping("modify")
    public ResponseBean modify(@RequestBody Map<String, String> map) {
        UpdateWrapper updateWrapper = BaseSelectSymbol.update_Analysis(map);
        boolean update = super.update(updateWrapper);
        return StatusCode.successResponseBean(update);
    }

    @RequiresAuthentication
    @ApiOperation(value = "根据列表id查询音乐")
    @ApiOperationSupport(params = @DynamicParameters(name = "playUUID查询",properties = {
            @DynamicParameter(name = "playUUID", value = "播放列表id", example = "e788e38873a6ab2a07dbef1ad61527f7", required = true, dataTypeClass = MultipartFile.class),
    }))
    @RequestMapping("muiscByPlayid")
    public ResponseBean muiscByPlayid(@RequestBody Map<String, String> map) {
        SymbolPlus symbolPlus = BaseSelectSymbol.select_Analysis(map);
        IPage<SqMusic> page = new Page<>(symbolPlus.getPageindex(),symbolPlus.getPagesize());

        List<SqMusic> playUUIDk = super.sService.MuiscByPlayid(page,map.get("playUUID"));
        return StatusCode.successResponseBean(playUUIDk);

    }
    @RequestMapping("addMusicToPlayList")
    @RequiresAuthentication
    @ApiOperation(value = "向列表中添加歌曲")
    @ApiOperationSupport(params = @DynamicParameters(name = "playUUID查询",properties = {
            @DynamicParameter(name = "playUUID", value = "播放列表id", example = "e788e38873a6ab2a07dbef1ad61527f7", required = true, dataTypeClass = MultipartFile.class),
            @DynamicParameter(name = "MusicUUID", value = "歌曲id", example = "e788e38873a6ab2a07dbef1ad61527f7", required = true, dataTypeClass = MultipartFile.class),
    }))
    public ResponseBean addMusicToPlayList(@RequestBody Map<String, String> map) {
        return  StatusCode.successResponseBean(super.sService.addMusicToPlayList(map.get("playUUID"),map.get("MusicUUID")));
    }


}
